package com.easy;

import java.util.*;

/**
 * Created by Lxk on 2019/7/14.
 */
public class Solution500 {
    private static List<Set<Character>> list = new ArrayList<>();

    static {
        Set<Character> line = new HashSet<>();
        Character[] arr = {'q','w','e','r','t','y','u','i','o','p'};
        List<Character> chs = Arrays.asList(arr);
        line.addAll(chs);
        Set<Character> line2 = new HashSet<>();
        Character[] arr2 = {'a','s','d','f','g','h','j','k','l'};
        List<Character> chs2 = Arrays.asList(arr2);
        line2.addAll(chs2);
        Set<Character> line3 = new HashSet<>();
        Character[] arr3 = {'z','x','c','v','b','n','m'};
        List<Character> chs3 = Arrays.asList(arr3);
        line3.addAll(chs3);
        list.add(line);
        list.add(line2);
        list.add(line3);
    }

    boolean isOneLine(String word){
        char[] chs = word.toCharArray();
        Set<Integer> lineCountSet = new HashSet<>();
        for(Character c : chs){
            for(int i = 0;i < list.size(); i++){
                if(list.get(i).contains(c)){
                    lineCountSet.add(i);
                    if(lineCountSet.size()>1){
                        return false;
                    }
                }
            }
        }
        return true;
    }

    public String[] findWords(String[] words) {
        List<String> resList = new ArrayList<>();
        for(String word : words){
            if(isOneLine(word)){
                resList.add(word);
            }
        }
        String[] strs = new String[resList.size()];
        for(int i = 0;i < resList.size(); i++){
            strs[i] = resList.get(i);
        }
        return strs;
    }

}
